<?php
namespace app\controllers;

use app\models\PstypeRecorder;

class PstypeController extends BaseController{
    public function actionAdd(){
        $post = \Yii::$app->request->post('form');
        $ps_type = intval($post['ps_type']);
        if(empty($post['ps_name'])){
            $result  =['result'=>0,'data'=>'配送名称不能为空'];
            return json_encode($result,JSON_NUMERIC_CHECK);
        }
        $model = PstypeRecorder::find()->where('ps_type=:ps_type',[':ps_type' => $ps_type])->one();
        if(empty($model)){
            $model = new PstypeRecorder();
            $model->ps_type=$ps_type;
        }
        $model->ps_name = $post['ps_name'];
        $model->content = $post['content'];
        $model->addtime = time();
        $model->is_type = intval($post['is_type']);
        $model->ps_price = abs(doubleval($post['ps_price']));
        $model->save(false);
        $result = ['result'=>1,'data'=>'操作成功'];
        return json_encode($result,JSON_NUMERIC_CHECK);
    }

    public function actionOne(){
        $type = \Yii::$app->request->post('type');
        $model = PstypeRecorder::find()->where('ps_type=:type',[':type' => $type])->asArray()->one();
        if(empty($model)){
            $result = ['result'=>0,'data'=>'不存在相关内容，请重新操作'];
            return json_encode($result,JSON_NUMERIC_CHECK);
        }
        $model['addtime'] = date('Y-m-d H:i:s',$model['addtime']);
        $result = ['result'=>1,'data'=>$model];
        return json_encode($result);
    }
}
